<template>
  <div class="fangying-container" :class="[fontFace]" ref="fyRef">
    <v-expand-transition>
      <div class="qr-zhezhao" v-if="qrisShow">
        <QrMa class="vue-qr" :id="id" />
      </div>
    </v-expand-transition>
    <v-expand-transition>
      <div v-if="refreshShow" class="btn-refresh">
        <div>连接码：{{ id }}</div>
        <div>
          <v-btn @click="handleRefresh" icon>
            <j-icon size="20">mdi-refresh</j-icon>
          </v-btn>
        </div>
        <div>
          <v-btn @click="handleFullscreen" icon>
            <j-icon size="20" v-if="ifFullScreen">mdi-fullscreen-exit</j-icon>
            <j-icon size="20" v-else>mdi-fullscreen</j-icon>
          </v-btn>
        </div>
      </div>
    </v-expand-transition>
    <span
      class="show-title"
      :style="`color: ${this.fontColor};font-size:${this.fontSize - 20 < 20 ? 20 : this.fontSize - 20}px`"
      v-if="showTitle"
    >
      {{ shijiName }} 《{{ title }}》</span
    >
    <!-- && messageTypeEnum.ZM === messageType -->
    <swiper
      v-show="
        pagePattern == pagePatternEnum.pageTurning &&
        (messageTypeEnum.ZM === messageType || messageTypeEnum.JW === messageType)
      "
      ref="mySwiper"
      class="h-100"
      :class="[`text-${textAlign}`]"
      :style="`-webkit-text-stroke-color:${textStorkeColor};-webkit-text-stroke-width:${textStorkeWidth}px`"
      :options="swiperOptions"
    >
      <swiper-slide
        class="h-100 d-flex flex-wrap align-items-center justify-content-center"
        :class="[firstSliderBgImg, `text-shadow-${textShadow}`]"
      >
        <div
          class="h1 w-100 slide-text slide-text-title px-2"
          :class="[`${pageIndex === 0 ? 'active' : ''}`]"
          :style="titleStyle"
        >
          {{ zhutiTitle }}
        </div>
      </swiper-slide>
      <swiper-slide
        :class="[bgImageClass, `text-shadow-${textShadow}`]"
        class="h-100 d-flex flex-wrap align-items-center justify-content-center"
      >
        <!-- <div class="h1 w-100 slide-text slide-text-title px-2"  :style="titleStyle">

        </div> -->
        <div
          class="h1 w-100 slide-text slide-text-title px-2"
          :class="[`${pageIndex === 1 ? 'active' : ''}`]"
          :style="titleStyle"
        >
          <span :style="textStyle">{{ shijiName }}</span>
          <br />
          {{ title }}
        </div>
      </swiper-slide>
      <swiper-slide
        :class="[bgImageClass, `text-shadow-${textShadow}`]"
        class="h-100 d-flex flex-wrap align-items-center justify-content-center"
        v-for="(c, i) in createRow"
        :key="i"
      >
        <div
          ref="wrapRef"
          class="h-100 w-100 d-flex"
          :class="[messageTypeEnum.JW === messageType ? 'overflow-auto' : 'align-center']"
        >
          <p
            class="w-100 slide-text px-2"
            :style="{
              ...textStyle,
              textAlign: messageTypeEnum.JW === messageType ? 'left' : textStyle.textAlign,
            }"
            :class="[`${pageIndex === i + 2 ? 'active' : ''}`]"
          >
            <span
              v-for="(t, x) in c"
              :key="x"
              :style="`--index:${x / 8}s`"
              class="lyric-item"
              :class="[`${pageIndex === i + 2 ? 'active' : ''}`]"
              >{{ t }}<br
            /></span>
          </p>
        </div>
      </swiper-slide>
    </swiper>

    <JcAutoPlayLyric
      v-show="pagePattern == pagePatternEnum.autoplay && messageTypeEnum.ZM === messageType"
      :zhutiTitleStyle="titleStyle"
      :textShadow="textShadow"
      :textStyle="textStyle"
      :bgImageClass="bgImageClass"
      :textAlign="textAlign"
      height="100vh"
      :clyric="clyric"
      :currentLyric="currentLyric"
    ></JcAutoPlayLyric>
    <gepu v-if="messageTypeEnum.GP === messageType" :src="gepuUrl" :options="gepuOptions" />
  </div>
</template>

<script>
import { Keyboard, Swiper as SwiperClass } from "swiper/js/swiper.esm.js";
import getAwesomeSwiper from "vue-awesome-swiper/dist/exporter";
import { mapActions, mapGetters } from "vuex";
import { watch, update, isFull } from "../../api/cloudbase-touying";

import "swiper/css/swiper.css";
import "./index.css";
import CONFIG, { pagePatternEnum, messageType as messageTypeEnum } from "./config";
import QrMa from "./components/QrMa.vue";
import JcScrollLyric from "./components/JcScrollLyric.vue";
import mqttMinis from "./mqttMinis";
import JcAutoPlayLyric from "./components/JcAutoPlayLyric.vue";
import JcJwTypeSwiper from "./components/JcJwTypeSwiper.vue";
import JcGePuFangYing from "./components/JcGePuFangYing.vue";
SwiperClass.use([Keyboard]);
const { Swiper, SwiperSlide } = getAwesomeSwiper(SwiperClass);
export default {
  name: "FangYing",
  props: ["id"],
  mixins: [mqttMinis],
  components: {
    Swiper,
    SwiperSlide,
    QrMa,
    JcAutoPlayLyric,
    JcJwTypeSwiper,
    gepu: JcGePuFangYing,
  },
  data() {
    return {
      info: {},
      zmIds: "",
      pageIndex: 0,
      swiperOptions: {
        speed: 0,
        keyboard: true,
      },
      ...CONFIG,
      refreshShow: false,
      ifFullScreen: false,
      pagePatternEnum,
      currentLyric: null,
      messageTypeEnum,
    };
  },
  mounted() {
    this.handleHover();
    this.createConnection({
      onConnect: () => {
        console.log("连接成功");
        this.doSubscribe(this.id);
      },
      onMessage: (message) => this.onMessage(message),
    });
  },
  beforeRouteEnter(to, from, next) {
    document.getElementsByTagName("html")[0].style.overflow = "hidden";
    next();
  },
  beforeRouteLeave(to, from, next) {
    document.getElementsByTagName("html")[0].style.overflow = "auto";
    next();
  },
  methods: {
    onMessage(doc) {
      console.log("doc-->", doc);
      this.pageIndex = doc.pageIndex || 0;
      this.swiper.slideTo(this.pageIndex);
      // if (this.qrisShow !== doc.qrisShow) {
      //   this.qrisShow = doc.qrisShow
      //   this.swiper.slideTo(0)
      // }
      if (this.rows !== doc.rows) {
        this.rows = doc.rows;
      }
      if (this.fontSize !== doc.fontSize) {
        this.fontSize = doc.fontSize;
      }
      if (this.lineHeight !== doc.lineHeight) {
        this.lineHeight = doc.lineHeight;
      }
      if (this.zhutiTitle !== doc.zhutiTitle) {
        this.zhutiTitle = doc.zhutiTitle;
      }
      if (this.titleFontSize !== doc.titleFontSize) {
        this.titleFontSize = doc.titleFontSize;
      }
      // if (this.titleStyle !== doc.titleStyle) {
      //   this.titleStyle = doc.titleStyle;
      // }
      if (this.firstSliderBgImg !== doc.firstSliderBgImg) {
        this.firstSliderBgImg = doc.firstSliderBgImg;
      }
      if (this.fontColor !== doc.fontColor) {
        this.fontColor = doc.fontColor;
      }
      if (this.bgColor !== doc.bgColor) {
        this.bgColor = doc.bgColor || "#4527A0";
        document.documentElement.style.setProperty("--touying-bg-color", this.bgColor || "#4527A0");
      }
      if (this.bgImageClass !== doc.bgImageClass) {
        this.bgImageClass = doc.bgImageClass || "bgjb";
      }
      if (this.textShadow !== doc.textShadow) {
        this.textShadow = doc.textShadow || 0;
      }
      if (this.zmIds !== doc.zmIds) {
        this.zmIds = doc.zmIds;
        this.selectLyric(doc.zmIds);
      }
      // textAlign
      if (this.textAlign !== doc.textAlign) {
        this.textAlign = doc.textAlign || this.textAlign;
      }
      if (this.textStorkeWidth !== doc.textStorkeWidth) {
        this.textStorkeWidth = doc.textStorkeWidth || this.textStorkeWidth;
      }
      if (this.textStorkeColor !== doc.textStorkeColor) {
        this.textStorkeColor = doc.textStorkeColor || this.textStorkeColor;
      }
      if (this.fontFace !== doc.fontFace) {
        this.fontFace = doc.fontFace || this.fontFace;
      }
      if (this.scrollProgress !== doc.scrollProgress) {
        this.scrollProgress = doc.scrollProgress || this.scrollProgress;
        if (doc.messageType === messageTypeEnum.JW && this.currentWrapRef) {
          this.currentWrapRef.scrollTo(0, this.currentWrapRef.scrollHeight * doc.scrollProgress);
        }
      }
      if (this.lyric !== doc.lyric) {
        this.lyric = doc.lyric;
      }
      if (this.title !== doc.title) {
        this.title = doc.title;
      }

      if (this.showTitle !== doc.showTitle) {
        this.showTitle = doc.showTitle;
      }
      if (this.shijiName !== doc.shijiName) {
        this.shijiName = doc.shijiName;
      }

      if (doc.messageType && this.messageType !== doc.messageType) {
        this.messageType = doc.messageType;
      }
      if (doc.messageType && this.jwLongText !== doc.jwLongText) {
        this.jwLongText = doc.jwLongText;
      }

      this.currentLyric = doc.currentLyric || null;

      if (doc.gepuUrl && this.gepuUrl !== doc.gepuUrl) {
        this.gepuUrl = doc.gepuUrl;
      }

      this.gepuOptions = doc.gepuOptions || null;

      // this.gepuBase64 = doc.gepuBase64

      try {
        this.pagePattern = doc.pagePattern;
      } catch (err) {}
    },
    handleRefresh() {
      this.qrisShow = !this.qrisShow;
      // update(this.id, {
      //   field: 'qrisShow',
      //   value: true,
      // })
    },
    handleHover() {
      window.addEventListener("mousemove", this.handleMousemove.bind(this));
    },
    handleMousemove(e) {
      if (e.clientY <= 100) {
        this.refreshShow = true;
      } else {
        this.refreshShow = false;
      }
    },
    handleFullscreen() {
      const elem = this.$refs.fyRef;
      if (elem.requestFullscreen) {
        this.ifFullScreen ? document.exitFullscreen() : elem.requestFullscreen();
        this.ifFullScreen = !this.ifFullScreen;
      } else {
        alert("请按功能键 F11 全屏网页");
      }
    },
    ...mapActions({
      selectLyric: "audio/selectLyric",
    }),
  },
  computed: {
    ...mapGetters(["clyric"]),
    // 生成行和列
    createRow() {
      const textrows = this.lyric.split("\n").filter((i) => !(i == "" || /^\r$/.test(i)));
      let tmp = [];
      const rows = [];
      textrows.forEach((t, i) => {
        if (i % this.rows === 0 && tmp.length > 0) {
          rows.push(tmp);
          tmp = [];
        }
        tmp.push(t);
      });

      rows.push(tmp);
      return rows;
    },
    swiper() {
      return this.$refs.mySwiper.$swiper;
    },
    textStyle() {
      return { lineHeight: `${this.lineHeight}px`, fontSize: `${this.fontSize}px`, color: `${this.fontColor}` };
    },
    titleStyle() {
      return { fontSize: `${this.titleFontSize}px`, color: `${this.fontColor}` };
    },
    fullscreenIcon() {
      return this.ifFullScreen ? "mdi-fullscreen-exit" : "mdi-fullscreen";
    },
    currentWrapRef() {
      return this.$refs.wrapRef[this.pageIndex - 2] || null;
    },
  },
};
</script>

<style scoped>
.fangying-container {
  width: 100vw;
  height: 100vh;
}

.qr-zhezhao {
  width: 100vw;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.8);
  position: absolute;
  z-index: 2;
  display: flex;
  justify-content: center;
  align-items: center;
}

.vue-qr {
  z-index: 2;
}

.btn-refresh {
  position: absolute;
  z-index: 2;
  top: 10px;
  right: 10px;
  color: #fff;
  display: flex;
  align-items: center;
}

.show-title {
  display: block;
  position: absolute;
  top: 10px;
  left: 10px;
  background-color: transparent;
  z-index: 20;
}

.transition {
  /* transition: all 0.2s linear; */
}

.slide-text {
  opacity: 1;
}

.slide-text.active {
  opacity: 1;
}

.lyric-item {
  opacity: 0;
  /* transition-delay: calc(var(--index)); */
  display: block;
}

.lyric-item.active {
  opacity: 1;
}
</style>
